<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

/**
 * 系统菜单管理控制器
 * @package App\Http\Controllers\Admin
 */
class SysMenuController extends BaseController
{
    private $menuSer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
        $this->menuSer = new \App\Services\SysMenuService();
    }

    /**
     * 菜单管理首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->ajax()){
            $groupId = $request->get('group_id', 0);
            $menuList = $this->menuSer->getMenuList($groupId);
            foreach ($menuList as $k => $v){
                $v['text'] = $v['menu_name'];
                $v['icon'] = $v['icon_class'];
                $v['tags'] = ['排序：'.$v['order_num']];
                $v['route_params'] = $v['route_params'] ? http_build_query($v['route_params']) : '';
                $menuList[$k] = $v;
            }

            //树结构菜单列表
            $tree = new \App\Services\TreeService('menu_id', 'parent_id', 'nodes');
            $tree->load($menuList);
            $treeMenuList = $tree->deepTree();
            $data['menuList'] = $treeMenuList ? $treeMenuList : [];
            return $data;
        }
        //全部分组数据
        $this->viewData['groupList'] = $this->menuSer->getGroupList();
        //权限树结构菜单列表
        $permSer = new \App\Services\SysPermissionService();
        $permList = $permSer->getChoiceList();
        $tree          = new \App\Services\TreeService('perm_id', 'parent_id', 'child');
        $tree->nameKey = 'display_name';
        $tree->valKey = 'perm_id';
        $tree->load($permList);
        $permOptionHtml = '<option value="0">请选择</option>';
        $permOptionHtml .= $tree->buildOptions();
        $this->viewData['permOptionHtml'] = $permOptionHtml;

        return view('admin.sysmenu.index', $this->viewData);
    }

    /**
     * 保存、添加菜单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function save(\App\Http\Requests\AdminSysMenuPost $request)
    {
        try {
            $rs = $this->menuSer->saveMenu($request);
            if ($rs) {
                return $this->showMsg('success', ($request->input('menu_id') ? '编辑' : '添加') . '成功', [], 'admin.sysmenu.index');
            }
            throw new \Exception(($request->input('menu_id') ? '编辑' : '添加') . '菜单失败');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 删除菜单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        try {
            $id = $request->input('menu_id', 0);
            if (!$id) {
                throw new \Exception('请指定要删除的菜单');
            }
            $rs = $this->menuSer->del($id);
            if (!$rs) {
                throw new \Exception('删除失败');
            }
            return $this->showMsg('success', '删除成功');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }
}
